home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_10 / 2n10016a < prev    next >
Text File  |  1991-04-03  |  2KB  |  77 lines

  1. ;***    AllocateSpinLock - Allocate Spin (short term) Lock Object.
  2. ;
  3. ;    This macro is used to allocate a spin lock and store its
  4. ;    handle in a specified location.  In the actual implementation,
  5. ;    this macro doesn't even call a package, but resets a spinlock
  6. ;    to a known state.  The place where we store the handle IS the
  7. ;    spinlock.
  8. ;
  9. ;    Usage:        AllocateSpinLock lock
  10.  
  11. AllocateSpinLock MACRO lck
  12.     mov    lck, 0            ; initialize the lock.
  13.     ENDM
  14.  
  15. ;***    DeallocateSpinLock - Deallocate Spin (short term) Lock Object.
  16. ;
  17. ;    This macro is used to deallocate a spin lock and return
  18. ;    it to the system.  In this implementation, since the user's
  19. ;    handle storage actually serves as the lock itself, we don't
  20. ;    do anything here at all.  If we change the implementation, then
  21. ;    the source can just be recompiled and the DeallocateSpinLock
  22. ;    calls will do something meaningful, like deallocating pool.
  23. ;
  24. ;    Usage:        DeallocateSpinLock lock
  25.  
  26. DeallocateSpinLock MACRO lck
  27.     mov    lck, 0            ; reset the lock.
  28.     ENDM
  29.  
  30. ;***    AcquireSpinLock - Acquire Spin (short term) Lock.
  31. ;
  32. ;    This macro is used to acquire a spin lock in a multiprocessor
  33. ;    system so that access to a mutually-exclusive object can be
  34. ;    controlled in an MP-safe way.  To setup a spinlock, initialize
  35. ;    it with the AllocateSpinLock macro.  Then to acquire the lock,
  36. ;    use this macro.  To free up the lock when you're done, use the
  37. ;    ReleaseSpinLock macro.
  38. ;
  39. ;    Usage:        AcquireSpinLock lock [, scratch-reg]
  40.  
  41. AcquireSpinLock MACRO lck, scr
  42.     IF    MULTIPROCESSOR
  43.     IFIDN    <scr>,<>
  44.     push    ax
  45.     mov    ax, 1
  46. @@:    xchg    lck, ax
  47.     or    ax, ax
  48.     jnz    @b
  49.     pop    ax
  50.     ELSE
  51.     mov    scr, 1
  52. @@:    xchg    lck, scr
  53.     or    scr, scr
  54.     jnz    @b
  55.     ENDIF
  56.     ENDIF
  57.     ENDM
  58.  
  59. ;***    ReleaseSpinLock - Release Spin (short term) Lock.
  60. ;
  61. ;    This macro is used to release a spin lock in a multiprocessor
  62. ;    system so that access to a mutually-exclusive object can be
  63. ;    controlled in an MP-safe way.  To setup a spinlock, initialize
  64. ;    it with the AllocateSpinLock macro.  To acquire the lock, use
  65. ;    AcquireSpinLock.  To free up the lock when you're done, use
  66. ;    the ReleaseSpinLock macro.
  67. ;
  68. ;    Usage:        ReleaseSpinLock lock
  69.  
  70. ReleaseSpinLock MACRO lck
  71.     IF    MULTIPROCESSOR
  72.     mov    lck, 0
  73.     ENDIF
  74.     ENDM
  75.  
  76. Figure 1.  Multiprocessor-Safe Spinlock Macros for 80x86 Assembly Language.
  77.